package com.skyline.base.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.skyline.base.dao.ShareDao;
import com.skyline.base.mapper.ShareMapper;
import com.skyline.base.model.Share;
import com.skyline.base.type.ShareType;
import com.skyline.common.bean.Page;
import com.skyline.common.cache.annotation.Cache;
import com.skyline.common.cache.annotation.CacheCategoryType;
import com.skyline.common.cache.annotation.CacheDelete;
import com.skyline.common.cache.annotation.Fk;
import com.skyline.common.cache.annotation.Param;

@Repository("shareDao")
public class ShareDaoImpl extends BaseDaoImpl implements ShareDao {

	@Value("${ShareDao.insertShare}")
	private String insertShareSql;

	@Value("${ShareDao.queryCountBySharerTypeResource}")
	private String queryCountBySharerTypeResourceSql;

	@Value("${ShareDao.queryShareBySharerId}")
	private String queryShareBySharerIdSql;

	@Value("${ShareDao.queryShareBySharerIdAndType}")
	private String queryShareBySharerIdAndTypeSql;

	@Value("${ShareDao.queryShareById}")
	private String queryShareByIdSql;

	@Value("${ShareDao.updateShareNum}")
	private String updateShareNumSql;

	@Value("${ShareDao.deleteShare}")
	private String deleteShareSql;

	@Value("${ShareDao.updateUp}")
	private String updateUpSql;

	@Value("${ShareDao.updateDown}")
	private String updateDownSql;

	@Value("${ShareDao.updateCommentNumAdd}")
	private String updateCommentNumAddSql;

	@Value("${ShareDao.updateCommentNumMinus}")
	private String updateCommentNumMinusSql;

	@Override
	@CacheDelete(type = CacheCategoryType.SHARE)
	public Long insertShare(@Fk Long sharerId, String sharerNickname, String sharerPortrait, ShareType shareType, Long resourceId,
			String resourceTitle, String resourceDigest, Long resourceOwnerId, String resourceOwnerNickname, String resourceOwnerPortrait,
			Long resourceSourceId, String resourceSoureceName) {
		return insertWithIdReturn(insertShareSql, sharerId, sharerNickname, sharerPortrait, shareType.toString(), resourceId,
				resourceTitle, resourceDigest, resourceOwnerId, resourceOwnerNickname, resourceOwnerPortrait, resourceSourceId,
				resourceSoureceName);
	}

	@Override
	@Cache(keyPattern = "share-l-:uid", type = CacheCategoryType.SHARE, pagination = true)
	public List<Share> queryShareBySharerId(@Param("uid") Long sharerId, Page page) {
		return getPaginationResult(queryShareBySharerIdSql, page, ShareMapper.getMapper(), sharerId);
	}

	@Override
	@Cache(keyPattern = "share-l-:uid-:type", type = CacheCategoryType.SHARE, pagination = true)
	public List<Share> queryShareBySharerIdAndType(@Param("uid") Long sharerId, @Param("type") ShareType shareType, Page page) {
		return getPaginationResult(queryShareBySharerIdAndTypeSql, page, ShareMapper.getMapper(), sharerId, shareType.toString());
	}

	@Override
	@Cache(keyPattern = "share-:id", type = CacheCategoryType.SHARE)
	public Share queryShareById(@Param("id") Long id) {
		List<Share> share = jdbcTemplate.query(queryShareByIdSql, ShareMapper.getMapper(), id);
		if (share != null && share.size() > 0) {
			return share.get(0);
		} else {
			return null;
		}
	}

	@Override
	@Cache(keyPattern = "share-i-:uid-:type-:rid", type = CacheCategoryType.SHARE)
	public Integer queryCountBySharerTypeResource(@Param("uid") Long sharerId, @Param("type") ShareType shareType,
			@Param("rid") Long resourceId) {
		return jdbcTemplate.queryForInt(queryCountBySharerTypeResourceSql, sharerId, shareType.toString(), resourceId);
	}

	@Override
	@CacheDelete(type = CacheCategoryType.SHARE)
	public void deleteShare(Long id, @Fk Long actionerId) {
		// TODO:还未测试
		jdbcTemplate.update(deleteShareSql, id, actionerId);
	}

	@Override
	public void updateUp(Long id) {
		// TODO:还未测试
		jdbcTemplate.update(updateUpSql, id);

	}

	@Override
	public void updateDown(Long id) {
		// TODO:还未测试
		jdbcTemplate.update(updateDownSql, id);
	}

	@Override
	@CacheDelete(type = CacheCategoryType.SHARE)
	public void updateShareNum(ShareType shareType, Long resourceId, @Fk Long ownerId) {
		// TODO:还未测试
		String sql = genSql(updateShareNumSql, shareType.toString().toLowerCase());
		jdbcTemplate.update(sql, resourceId);
	}

	@Override
	public void updateCommentNumAdd(Long id) {
		jdbcTemplate.update(updateCommentNumAddSql, id);
	}

	@Override
	public void updateCommentNumMinus(Long id) {
		jdbcTemplate.update(updateCommentNumMinusSql, id);
	}

}
